package cn.wh.beans.factory.support;

import cn.wh.beans.PropertyValues;
import cn.wh.beans.factory.BeanFactory;
import cn.wh.beans.factory.config.BeanDefinition;
import cn.wh.beans.factory.config.BeanReference;

import java.sql.Timestamp;

/**
 * @Author wanghao
 * @Date 2022-08-21 0:58
 * @Description: TODO
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
    @Override
    public Object getBean(String beanName) {
        return doGetBean(beanName, null);
    }

    @Override
    public Object getBean(String beanName, Object... args) {
        return doGetBean(beanName, args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) {
        return (T) getBean(name);
    }

    public <T> T doGetBean(String beanName, Object... args) {
        //查询单例注册表中是否含有bean
        T bean = (T) getSingletonBean(beanName);
        if (bean == null) {
            BeanDefinition beanDefinition = getBeanDefinition(beanName);
            bean = (T) createBean(beanDefinition, beanName, args);
        }
        return (T) bean;
    }

    protected abstract Object createBean(BeanDefinition beanDefinition, String beanName, Object... args);

    protected abstract BeanDefinition getBeanDefinition(String beanName);
}
